package com.dongdongshop.serviceimpl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbItemCatExample;
import com.dongdongshop.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class IntemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    //查询所有分类管理
    @Override
    public List<TbItemCat> findAll(Long id) {
        TbItemCatExample tce = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tce.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(tce);

        return tbItemCats;
    }

    //分类列表
    @Override
    public List<TbItemCat> selectById(Long id) {
        List<TbItemCat> list2 = new ArrayList<>();
        if (id <= 0){
            return null;
        }
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        List<TbItemCat> list = getList(tbItemCat,list2);
        return list;
    }

    public List<TbItemCat> getList(TbItemCat tbItemCat,List<TbItemCat> list){
        list.add(0,tbItemCat);
        if (tbItemCat.getParentId() == 0){
            return list;
        }
        TbItemCat tbItemCat1 = tbItemCatMapper.selectByPrimaryKey(tbItemCat.getParentId());
        return getList(tbItemCat1,list);
    }

    //增加
    @Override
    public Boolean insertItemCat(TbItemCat tbItemCat) {
        int i = tbItemCatMapper.insertSelective(tbItemCat);
        if (i > 0){
            return true;
        }
        return false;
    }

    //回显
    @Override
    public TbItemCat echoItemCatById(Long id) {
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        return tbItemCat;
    }

    //修改
    @Override
    public Boolean updateItemCat(TbItemCat tbItemCat) {
        try {
            int i = tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
            if (i > 0){
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //删除
    @Override
    public Boolean deleteById(Long[] ids) {
        try {
            for (int i = 0; i < ids.length; i++) {
                int i1 = tbItemCatMapper.deleteByPrimaryKey(ids[i]);
                if (i1 == 0){
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //获取typeid
    @Override
    public TbItemCat selectTypeId(Long id) {
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        return tbItemCat;
    }
}
